<?php

/**
 * 
 * @author yuxun
 *
 */
class ActivityCategoryController extends AdminBaseController {
	
	public function index(){
		$this->setTitle('大活动管理');
	
		$filter = new PubFilter();
		$grid   = new PubGrid();
		
		$classifyList = ActivityCategoryService::classifyList();
		$classifyList = array('' => '所有') + $classifyList;
	
		$filter->addFilter('classify', 'select', PubFunc::t('种类'), array('options' => $classifyList, 'value' => key($classifyList)));
		$filter->addFilter('activity_id', 'input', '大活动ID');
		$filter->addFilter('activity_name', 'input', '大活动名称');
		
		$url = PubFunc::buildUrl("/oss/?r=activityCategory/importGoods");
		$filter->addImport('活动导入',$url);
	
		$params = $filter->get();
		$page = $this->R('page', '1');
		$m = $this->R('m');
		$params['m'] = $m;
		$header = ActivityCategoryService::indexHeader();
		if(PubFunc::isAjax()){
			$result = ActivityCategoryService::indexData($params);
			$this->out = array(
					'count'     => $result['count'],
					'data'      => $result['data'],
					'result'    => true
			);
			$this->outType = 'json';
		}else{
			$order  = 'id';
			$url = PubFunc::ajaxUrl("?r=activityCategory/index", $params);
			$grid->addGrid($header, $url, array(
					'name'=>'数据',
					'order'=>array($order, 'desc'),
					'type'=>'activityCategory/index',
					'length'=>50,
					'page'=>$page
			));
	
			$this->out['filter'] = $filter->show();
			$this->out['grid'] = $grid->show();
			
			$this->out['downUrl'] = '/public/template/activityCategory.xlsx';
		}
	}
	
	public function add(){
		$this->setTitle('添加大活动');
		$classifyList = ActivityCategoryService::classifyList();
		$this->out['classifyList'] = $classifyList;
	}
	
	public function doAdd(){
		$count = ActivityCategoryService::repeat(0, 'activity_id', $this->R('activity_id'),$this->R('g'));
		if($count > 0){
			$this->showMessage('该大活动ID已经存在');
		}else{
			$result = ActivityCategoryService::doAdd($_POST);
	
			if($result['state'] !== false){
				$this->showMessage($result['msg'], PubFunc::buildUrl("/oss/?r=activityCategory/index"));
			}else{
				$this->showMessage($result['msg']);
			}
		}	
	}
	
	public function edit(){
		$id = $this->R('id');
		$info = ActivityCategoryService::get($id);
		if(empty($info)) $this->showMessage('找不到该信息');
		
		$modOssActivity = new ModOssActivity();
		$activityObj = $modOssActivity->findAllByAttributes(array('category_id'=>$info['id']));
		$is_update_activity_id = 0; //可以修改
		if($activityObj){
			$is_update_activity_id = 1; //不可以修改
		}
		$this->out['is_update_activity_id'] = $is_update_activity_id;
		
		$this->setTitle('编辑', $info['activity_name']);
		$this->out['info'] = $info;
		
		$classifyList = ActivityCategoryService::classifyList();
		$this->out['classifyList'] = $classifyList;
		
		$backUrl = PubFunc::buildBackUrl("/oss/?r=activityCategory/index");  //返回路径
		$this->out['backUrl'] = $backUrl;
			
		$page = $this->R('page');
		$this->out['page'] = $page;
	}
	
	public function doEdit(){
		$id = $this->R('id');
		$count = ActivityCategoryService::repeat($this->R('id'), 'activity_id', $this->R('activity_id'),$this->R('g'));
		
		if($count > 0){
			$this->showMessage('该大活动ID已经存在');
		}else{
			$result = ActivityCategoryService::doEdit($id, $_POST);
	
			if($result['state'] !== false){
				$this->showMessage('编辑成功', PubFunc::buildBackUrl("/oss/?r=activityCategory/index"));
			}else{
				$this->showMessage('编辑失败:'.$result['msg']);
			}
		}
	
	}
	

	public function delete(){
		$id = $this->R('id');
	    $result = ActivityCategoryService::delete($id);
	    if($result['state']  !== false){
	    	$this->showMessage($result['msg'], PubFunc::buildBackUrl("/oss/?r=activityCategory/index"));
	    }else{
	    	$this->showMessage($result['msg'], PubFunc::buildBackUrl("/oss/?r=activityCategory/index"));
	    }	 
	}
	
	public function importGoods(){
		$upload = ActivityCategoryService::upload($_FILES);
		if($upload["status"] == 1){ //上传成功
			$filePath = $upload['data'];
			$data = LibExcel::import($filePath);
			$result = ActivityCategoryService::xlsAdd($data);
			$out = $result;
		}else{
			$out = $upload; //上传失败
		}
	
		$this->out = $out;
		$this->outType = 'json';
	}




}